package exp3;

/**
 * Created by 蜡笔小新丶 on 2017/11/6.
 */
public class HeapSort {

    public static void Sort(int[] a)  {
        int n = a.length;   int temp = 0;
        Display(a, "排序前 : ");
        for(int i=n/2; i>0; i--)
            Adjust(a, i-1, n);
        for(int i=n-2; i>=0; i--)   {
            temp = a[i+1];
            a[i+1] = a[0];
            a[0] = temp;
            Adjust(a, 0, i+1);
        }
        Display(a, "排序后: ");
    }
    public  static void Adjust(int[] a, int i, int n)  {
        int j = 0;
        int temp = 0;
        temp = a[i];
        j = 2 * i + 1;
        while(j <= n-1)   {
            if(j < n-1 && a[j] < a[j+1])
                j++;
            if(temp >= a[j])
                break;
            a[(j-1)/2] = a[j];
            j = 2 * j + 1;
        }
        a[(j-1)/2] = temp;
    }
    public static void Display(int[] a, String str)  {
        System.out.println(str);
        for(int i=0; i<a.length; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }
}
